//
//  LSCustomHeaderView.m
//  LookScore
//
//  Created by wr on 2017/12/8.
//  Copyright © 2017年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSCustomHeaderView.h"

@implementation LSCustomHeaderView


- (nullable instancetype)initWithReuseIdentifier:(nullable NSString *)reuseIdentifier  style:(KTableViewHeaderStyle )style {
    
    self = [super initWithReuseIdentifier:reuseIdentifier];
    
    if (self) {
       
        self.acrossWidthArr=[[NSMutableArray alloc]init];
        self.lineColor=[UIColor blackColor];
        self.leftTitleColor=[UIColor blackColor];
        self.rightTitleColor=LSTitleGrayColor2;
        self.selectedColor=LSSubColore5735e;
        self.spaceWidth=30;
        [self initHeaderView:style];//_init表示初始化方法
    }
    
    return self;
}


-(void)initHeaderView:(KTableViewHeaderStyle )style{
    
   
    UIView *lineView=[[UIView alloc]init];
    lineView.backgroundColor=self.lineColor;
    [self addSubview:lineView];
    self.lineView=lineView;
    
    if (style==KTableViewHeaderTitleStyle) {
       
        lineView.sd_layout.leftEqualToView(self).offset(LSSpaceLeftWidth1).centerYEqualToView(self).heightIs(18).widthIs(2.f);
        
        UILabel *timeLab=[[UILabel alloc]init];
        timeLab.font=LSFontSize12;
        timeLab.textColor=self.leftTitleColor;
        timeLab.textAlignment=NSTextAlignmentLeft;
        [self addSubview:timeLab];
        self.timeLab=timeLab;
        timeLab.sd_layout.leftSpaceToView(lineView, 8).rightEqualToView(self).offset(-((LSScreenWidth-30)*0.3+30)).centerYEqualToView(self).heightIs(20);
        
        UILabel *siteLabOne=[[UILabel alloc]init];
        siteLabOne.font=LSFontSize10;
        siteLabOne.textColor=self.rightTitleColor;
        siteLabOne.textAlignment=NSTextAlignmentLeft;
        [self addSubview:siteLabOne];
         self.siteLabOne=siteLabOne;
        siteLabOne.sd_layout.leftSpaceToView(timeLab,0).centerYEqualToView(self).heightIs(20).widthIs(((LSScreenWidth-30)*0.3)/3);
        
        
        UILabel *siteLabTwo=[[UILabel alloc]init];
        siteLabTwo.font=LSFontSize10;
        siteLabTwo.textColor=self.rightTitleColor;
        siteLabTwo.textAlignment=NSTextAlignmentLeft;
        [self addSubview:siteLabTwo];
         self.siteLabTwo=siteLabTwo;
        siteLabTwo.sd_layout.leftSpaceToView(siteLabOne,0).centerYEqualToView(self).heightIs(20).widthIs(((LSScreenWidth-30)*0.3)/3);
        
        UILabel *siteLabThree=[[UILabel alloc]init];
        siteLabThree.font=LSFontSize10;
        siteLabThree.textColor=self.rightTitleColor;
        siteLabThree.textAlignment=NSTextAlignmentLeft;
        [self addSubview:siteLabThree];
         self.siteLabThree=siteLabThree;
        siteLabThree.sd_layout.leftSpaceToView(siteLabTwo,0).centerYEqualToView(self).heightIs(20).widthIs(((LSScreenWidth-30)*0.3)/3);
        
        
    }else if (style==KTableViewHeaderScrollerStyle){
     lineView.sd_layout.leftEqualToView(self).offset(LSSpaceLeftWidth1).centerYEqualToView(self).heightRatioToView(self, 0.4).widthIs(2.f);
      
    }
    
    
}


/**
 KTableViewHeaderTitleStyle 显示时间

 @param siteArray
 */
-(void)setSiteArray:(NSArray *)siteArray{
    _siteArray=siteArray;
    self.siteLabOne.text=_siteArray[0];
     self.siteLabTwo.text=_siteArray[1];
     self.siteLabThree.text=_siteArray[2];
}


/**
 KTableViewHeaderScrollerStyle 可选择

 @param scrolArray 填充数组
 @param spaceWidth 间隙
 @param titleColor 字体颜色
 @param seletedColor 选中颜色
 */
-(void)setScrolArray:(nullable NSArray *)scrolArray spaceWidth:(CGFloat )spaceWidth titleColor:(nullable UIColor *)titleColor  seletedColor:(nullable UIColor *)seletedColor with:(NSInteger)selectedtag{
    
    _scrolArray=scrolArray;
    _spaceWidth=spaceWidth;
    _leftTitleColor=titleColor;
    _selectedColor=seletedColor;

    CGFloat allWidth=13;;
    CGFloat viewX=0;
    for (int i=0; i<_scrolArray.count; i++) {
        UIButton *button=[UIButton new];
        [button setTitle:_scrolArray[i] forState:UIControlStateNormal];
        [button setTitleColor:_leftTitleColor forState:UIControlStateNormal];
        [button setTitleColor:_selectedColor forState:UIControlStateSelected];
        button.titleLabel.font=LSFontSize13;
        button.tag=i;
        [button addTarget:self action:@selector(selectedEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        CGFloat width=[CommonClass textWidthByString:_scrolArray[i] sizeFont:13 layoutWidth:self.width spaceWidth:5];
        button.sd_layout.leftSpaceToView(self.lineView,allWidth).topEqualToView(_lineView).offset(-5).bottomEqualToView(self).offset(-10).widthIs(width);
       
         viewX=allWidth+(width-10)/2;
        
        allWidth+=_spaceWidth+width;
        
       
        
        [self.acrossWidthArr addObject:[NSString stringWithFormat:@"%.1f",viewX]];
        
        if (i == selectedtag) {
            self.segmentBtn=button;
            //           移动的线
            button.selected=YES;
        
            UIView *acrossView=[[UIView alloc]initWithFrame:CGRectMake(0, button.frame.size.height -1, width,1)];
            acrossView.backgroundColor=_selectedColor;
            [self addSubview:acrossView];
        acrossView.sd_layout.bottomEqualToView(self).offset(-5).widthIs(10).heightIs(2).leftSpaceToView(self.lineView,viewX);
            self.acrossView=acrossView;
            
        }
        
        
    }
    
}


-(void)selectedEvent:(UIButton *)sender{
    self.selectTag = sender.tag;
    NSString *acrossWidthS=self.acrossWidthArr[sender.tag];

    CGFloat acrossWidth=acrossWidthS.floatValue;

    if (sender.selected==NO) {
        
        sender.selected=!sender.selected;
        
        if (sender.selected==YES ) {
            
            for (UIButton *buttonBo in self.subviews) {
                if ([buttonBo isKindOfClass:[UIButton class]]) {
                
                    if (sender.tag==buttonBo.tag) {
                        
                        [buttonBo setTitleColor:_selectedColor forState:UIControlStateSelected];
                        buttonBo.selected=YES;
                        self.segmentBtn=buttonBo;
                      
                     
                        self.acrossView.sd_resetNewLayout.leftSpaceToView(self.lineView, acrossWidth).bottomEqualToView(self).offset(-5).widthIs(10).heightIs(2);
                        
                        [self layoutSubviews];
                        [self layoutIfNeeded];
                        
                        if (self.butonActionBlock) {
                            self.butonActionBlock(buttonBo.tag);
                        }
                     
                    }else{
                        
                        [buttonBo setTitleColor:_leftTitleColor forState:UIControlStateNormal];
                        buttonBo.selected=NO;
                        
                    }
                    
                }
                
            }
            
        }
        
    }
        
}


#pragma mark 🔥 setting
-(void)setLeftTitleColor:(UIColor *)leftTitleColor{
    _leftTitleColor=leftTitleColor;
    self.timeLab.textColor=_leftTitleColor;
    [self.segmentBtn setTitleColor:_selectedColor forState:UIControlStateNormal];
}
-(void)setRightTitleColor:(UIColor *)rightTitleColor{
    _rightTitleColor=rightTitleColor;
    self.siteLabOne.textColor=_rightTitleColor;
    self.siteLabTwo.textColor=_rightTitleColor;
    self.siteLabThree.textColor=_rightTitleColor;
    
}
-(void)setSelectedColor:(UIColor *)selectedColor{
    _selectedColor=selectedColor;
    self.acrossView.backgroundColor=_selectedColor;
    [self.segmentBtn setTitleColor:_selectedColor forState:UIControlStateSelected];
}
-(void)setSpaceWidth:(CGFloat)spaceWidth{
    _spaceWidth=spaceWidth;
    
}
-(void)setLineColor:(UIColor *)lineColor{
    _lineColor=lineColor;
    self.lineView.backgroundColor=_lineColor;
}



@end

#pragma mark 🔥 LSTopSegmentSortView

@implementation LSTopSegmentSortView{
    
    NSMutableArray * _btnArray;
}


- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        _btnArray = @[].mutableCopy;
    }
    
    return self;
}

-(void)setControlWidth:(CGFloat)controlWidth{
    _controlWidth=controlWidth;
    
}

- (void)setDataArray:(NSMutableArray *)dataArray{
    
    _dataArray = dataArray;
    
    [_btnArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton * btn  = (UIButton *)obj;
        [btn setTitle:dataArray[idx] forState:UIControlStateNormal];
    }];
}

-(void)addBottomArray:(NSArray *)bottomArray selectButton:(NSInteger )selectTag {
    
    _bottomArray =bottomArray;
    
  
    //1. 创建一个scrollview
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    //设置scrollView尺寸
    scrollView.x = 0;
    scrollView.y = 0;//CGRectGetMaxY(self.frame);
    scrollView.width = self.width;
    scrollView.height =self.frame.size.height;
    CGFloat width=70*(self.bottomArray.count);

    if (self.controlWidth<=0) {
//        根据width 来判断滚不滚动 （滚动 空间宽度就默认80 不滚比例平分）
        if(width<=self.width){
            width=self.width;
        }
         self.controlWidth=width/(self.bottomArray.count);
    }else{
//    自定义控件宽度 不可滚动
        width=self.controlWidth*(self.bottomArray.count);
    }
  
    //设置scrollView 相关显示属性
    scrollView.contentSize = CGSizeMake(width,0);
    scrollView.pagingEnabled = NO;
    scrollView.bounces = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView=scrollView;
    [self addSubview:scrollView];
    [self updateLayout];
    
   

    NSInteger titleInt=self.bottomArray.count;
    
    for (int i=0; i<titleInt; i++)
    {
    
        UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
        button.frame=CGRectMake(self.controlWidth*i, 0, self.controlWidth, scrollView.height-2);
        [button setTitle:self.bottomArray[i] forState:UIControlStateNormal];
        
        button.titleLabel.font = [UIFont systemFontOfSize: 14.0f];
        [button setTitleColor:self.unSelectedColor forState:UIControlStateNormal];
        [button setTitleColor:self.selectedColor forState:UIControlStateSelected];
        button.tag = i+1;
        button.titleLabel.numberOfLines=2;
        button.titleLabel.textAlignment=NSTextAlignmentCenter;
        [button addTarget:self action:@selector(segBottomActionClick:) forControlEvents:UIControlEventTouchUpInside];
        [scrollView addSubview:button];
        if (i==selectTag) {
            //           移动的线
            self.segmentBu=button;
            button.selected=YES;
            UIView *view=[[UIView alloc]initWithFrame:CGRectMake(self.controlWidth*(selectTag)+(self.controlWidth-10)/2, button.frame.size.height-2, 10,2)];
            view.backgroundColor=self.selectedColor;
            view.layer.masksToBounds=YES;
            view.layer.cornerRadius=1;
            [scrollView addSubview:view];
            self.moveView=view;
            
        }
        [_btnArray addObject:button];
  
    }
}


-(void)segBottomActionClick:(UIButton *)sender{
    
    if (sender.selected==NO) {
        
        sender.selected=!sender.selected;
        
        if (sender.selected==YES ) {
            
            for (UIButton *buttonBo in self.scrollView.subviews) {
                if ([buttonBo isKindOfClass:[UIButton class]]) {
                    
                    if (sender.tag==buttonBo.tag) {
                        
                        [buttonBo setTitleColor:self.selectedColor forState:UIControlStateSelected];
                        buttonBo.selected=YES;
                        self.moveView.frame=CGRectMake(self.controlWidth*(buttonBo.tag-1)+(self.controlWidth-10)/2,buttonBo.frame.size.height-2, 10,2);
                        if (self.butonActionBlock) {

                            self.butonActionBlock(sender.tag-1);
                        }
                        
                    }else{
                        
                        [buttonBo setTitleColor:self.unSelectedColor forState:UIControlStateNormal];
                        buttonBo.selected=NO;
                        
                    }
                    
                    
                }
  
                
            }
            
        }
        
    }
    
}


-(void)setSelectedColor:(UIColor *)selectedColor{
    _selectedColor=selectedColor;
    [self.segmentBu setTitleColor:self.selectedColor forState:UIControlStateSelected];
    self.moveView.backgroundColor=self.selectedColor;
    
}
-(void)setUnSelectedColor:(UIColor *)unSelectedColor{
    _unSelectedColor=unSelectedColor;
    [self.segmentBu setTitleColor:self.unSelectedColor forState:UIControlStateNormal];
    
}
@end
